<?php
namespace Api\Rds;

class Util{

    const URL_DEV = "http://rdstest.hrbbwx.com/api/";
    const URL_PRODUCT = "http://rds.i.huixinglicai.com/api/";

    const SUCC_CODE = "2000000";  //成功响应码

    public $code;
    public $message;

    public function request($uri, array $params){
        $url = (\Core\Env::isProductEnv() ? self::URL_PRODUCT : self::URL_DEV);
        $response = (new \S\Http($url))->request(\S\Http::METHOD_POST, $uri, $params);
        if (empty($response)) {
            $this->code = 5203001;
            $this->message = "empty response from rds";
        }
        $result = json_decode($response, true);
        if (empty($result)) {
            \S\Log\Logger::getInstance()->error(array($response));
            $this->code = 5203002;
            $this->message = "error response from rds";
        }
        if (self::SUCC_CODE != $result["retcode"]) {
            $this->code = $result['retcode'];
            $this->message = $result['msg'];
            return false;
        }else{
            $this->code = $result['retcode'];
            $this->message = $result['msg'];
            unset($result['retcode']);
            unset($result['msg']);
            return $result;
        }
    }
}